



filenames<-list.files(pattern="Terugroep_actie_status" )

#Get English name of download dates, order files by date
filedate<-str_match(filenames, "Terugroep_actie_status_(.*?).RData" )[,2]
filedate<-gsub("[Jj]anuari", "january",filedate )
filedate<-gsub("[Ff]ebruari", "february",filedate )
filedate<-gsub("[Mm]aart", "march",filedate )
filedate<-gsub("[Mm]ei", "may",filedate )
filedate<-gsub("[Jj]uni", "june",filedate )
filedate<-gsub("[Jj]uli", "july",filedate )
filedate<-gsub("[Aa]ugustus", "august",filedate )
filedate<-gsub("[Oo]ktober", "october",filedate )
Files<-cbind.data.frame(filenames,as.Date(dmy(filedate)),stringsAsFactors=FALSE)
colnames(Files)<-c("path", "date")
Files<- Files[order(Files$date),]
Files$order<-order(Files$date)
rm(filedate, filenames)

#Get Main file (the earliest we have), change name, add date
d<-1
load(Files$path[d])

#Drop dots from variable names
names(x)<-gsub("\\.", "",names(x))
#All lower case
names(x)<-tolower(names(x))
#Transform factor in character
i <- sapply(x, is.factor)
x[i] <- lapply(x[i], as.character)
rm(i)
#Change name
assign("Recall",x)
rm(x)
gc()


Recall$status<-NULL
Recall$codestatus<-NULL

for(j in 2:(nrow(Files))) {

#Load file to add
load(Files$path[j])


#Drop dots from variable names
names(x)<-gsub("\\.", "",names(x))
#All lower case
names(x)<-tolower(names(x))
#Transform factor in character
i <- sapply(x, is.factor)
x[i] <- lapply(x[i], as.character)
rm(i)
x$status<-NULL
x$codestatus<-NULL

Recall<-rbind(Recall, x)
i<-duplicated(Recall)
Recall<-subset(Recall, i==FALSE)
rm(i)

print(paste("ITERATION ", j, ", ",Files$date[j],  sep=""))

}

rm(x)
gc()

save(Recall, file="Kenteken_recalls.RData")

length(unique(Recall$referentiecoderdw))

load("Kenteken_recalls.RData")

load("Terugroep_actie_6 april 2019.RData")

load("Registration_merged.RData")



Main$rec_PDR<-NA
Main$rec_DAP<-NA
Main$rec_DMBR<-NA
Main$rec_DIE<-NA
Main$rec_DEG<-NA
Main$rec_codes<-NA



#Drop dots from variable names
names(x)<-gsub("\\.", "",names(x))

#All lower case
names(x)<-tolower(names(x))

#Transform factor in character
i <- sapply(x, is.factor)
x[i] <- lapply(x[i], as.character)

i<- grepl("/", x$publicatiedatumrdw)==FALSE & is.na(x$publicatiedatumrdw)==FALSE & x$publicatiedatumrdw!="" 
x$publicatiedatumrdw[i]<-gsub("^([[:digit:]]{4})([[:digit:]]{2})([[:digit:]]{2})$", "\\1/\\2/\\3" ,x$publicatiedatumrdw[i])

i<- grepl("/", x$datumaankondigingproducent)==FALSE & is.na(x$datumaankondigingproducent)==FALSE & x$datumaankondigingproducent!="" 
x$datumaankondigingproducent[i]<-gsub("^([[:digit:]]{4})([[:digit:]]{2})([[:digit:]]{2})$", "\\1/\\2/\\3" ,x$datumaankondigingproducent[i])

i<- grepl("/", x$datummeldingbijrdw)==FALSE & is.na(x$datummeldingbijrdw)==FALSE & x$datummeldingbijrdw!="" 
x$datummeldingbijrdw[i]<-gsub("^([[:digit:]]{4})([[:digit:]]{2})([[:digit:]]{2})$", "\\1/\\2/\\3" ,x$datummeldingbijrdw[i])

i<- grepl("/", x$datuminformereneigenaar)==FALSE & is.na(x$datuminformereneigenaar)==FALSE & x$datuminformereneigenaar!="" 
x$datuminformereneigenaar[i]<-gsub("^([[:digit:]]{4})([[:digit:]]{2})([[:digit:]]{2})$", "\\1/\\2/\\3" ,x$datuminformereneigenaar[i])

i<- grepl("/", x$datumeigenarengeu00efnformeerd)==FALSE & is.na(x$datumeigenarengeu00efnformeerd)==FALSE & x$datumeigenarengeu00efnformeerd!="" 
x$datumeigenarengeu00efnformeerd[i]<-gsub("^([[:digit:]]{4})([[:digit:]]{2})([[:digit:]]{2})$", "\\1/\\2/\\3" ,x$datumeigenarengeu00efnformeerd[i])

Reclist<-x

Reclist<-Reclist[order(Reclist$referentiecoderdw),]
rm(x,i)


#Get recall list



rc<-unique(Reclist$referentiecoderdw)

#start loop
for(j in 1:1844) {


#Get all plates belonging to recall j 
Temp<-subset(Recall, referentiecoderdw==rc[j])


i<-Main$kenteken %in% Temp$kenteken

#To all plates i that received recall j, add date 
Main$rec_PDR[i]<-paste(Main$rec_PDR[i], Reclist$publicatiedatumrdw[j], sep=";")
Main$rec_DAP[i]<-paste(Main$rec_DAP[i], Reclist$datumaankondigingproducent[j], sep=";")
Main$rec_DMBR[i]<-paste(Main$rec_DMBR[i], Reclist$datummeldingbijrdw[j], sep=";")
Main$rec_DIE[i]<-paste(Main$rec_DIE[i], Reclist$datuminformereneigenaar[j], sep=";")
Main$rec_DEG[i]<-paste(Main$rec_DEG[i], Reclist$datumeigenarengeu00efnformeerd[j], sep=";")
Main$rec_codes[i]<-paste(Main$rec_codes[i], rc[j], sep=";")
rm(i, Temp)
print(paste("Iteration ", j, " ", rc[j], " ended", sep=""))
}

save(Main, file="Registration_recalls_merged.RData")



